/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.memory;

import java.util.Enumeration;
import java.util.List;

import org.vnetcon.blobdb.database.backend.BaseTableInterface;
import org.vnetcon.blobdb.database.backend.PersistenceManagerInterface;
import org.vnetcon.blobdb.database.backend.QueryInterface;
import org.vnetcon.blobdb.database.backend.QueueEntityInterface;
import org.vnetcon.blobdb.database.backend.TransactionInterface;
import org.vnetcon.blobdb.database.backend.memory.mdo.PMF;
import org.vnetcon.blobdb.database.backend.memory.mdo.Table;
import org.vnetcon.blobdb.database.backend.memory.mdo.TableData;
import org.vnetcon.blobdb.database.backend.memory.mdo.TableID;

public class MemoryPersistenceManager implements PersistenceManagerInterface {

	MemoryTransaction trans;
	
	
	public MemoryPersistenceManager(MemoryTransaction trans){
		this.trans = trans;
	}
	
	@Override
	public QueryInterface newQuery(Class<?> clazz) {
		MemoryQuery mq = new MemoryQuery(this.trans);
		return mq;
	}

	@Override
	public void makePersistent(Object oInterface) {
		Object o = ((BaseTableInterface)oInterface).getNativeTable();

		if(this.trans.isTransactionBegin()){
			if(o instanceof Table){
				trans.addToTransaction((Table)o);
			}
			if(o instanceof TableData){
				trans.addToTransaction((TableData)o);
			}
			if(o instanceof TableID){
				trans.addToTransaction((TableID)o);
			}
		}else{
			if(o instanceof Table){
				((Table)o).save();
			}
			if(o instanceof TableData){
				((TableData)o).save();
			}
			if(o instanceof TableID){
				((TableID)o).save();
			}
		}
	}

	@Override
	public TransactionInterface currentTransaction() {
		return trans;
	}

	@Override
	public void close() {
		this.trans.close();
	}

	@Override
	public void deletePersistentAll(List<?> dels) {
		int i = 0;
		while(i < dels.size()){
			this.deletePersistent(dels.get(i));
			i++;
		}
	}

	@Override
	public void deletePersistent(Object o) {
		
		if(o instanceof MemoryTable){
			MemoryTable memTable = (MemoryTable)o;
			Table nativeTable = (Table)memTable.getNativeTable();
			int iTableId = nativeTable.getTableID();
			PMF.hTable.remove(iTableId);
			Enumeration<TableData> e = PMF.hTableData.elements();
			while(e.hasMoreElements()){
				TableData tdBuf = e.nextElement();
				PMF.hTableData.remove(tdBuf.getTableID());
			}
		}
		
	}

	public void finalize() {
		this.trans.close();
	}

	@Override
	public void waitForMyTurn(long timestamp, int updateDelay, int tableId, int tableDataId) throws Exception {
	}

	@Override
	public void doneWithUpdate() {
	}
	
}
